package com.alks.function.service.impl.common;

import com.alks.common.data.thread.UserIdThread;
import com.alks.entity.data.entity.PcMonthHoliday;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.mapper.biocontrol.deptwork.BC_DW_PcMonthHolidayMapper;
import com.alks.function.service.common.IHolidayService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author MS
 * @since 2024/9/20下午5:16
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class HolidayServiceImpl extends ServiceImpl<BC_DW_PcMonthHolidayMapper, PcMonthHoliday> implements IHolidayService {
    private final BC_DW_PcMonthHolidayMapper holidayMapper;

    @Override
    public void add(LocalDateTime date) {
        String companyId = UserIdThread.get().getCompanyId();
        /*判断是否重复*/
        Integer count = lambdaQuery()
                .eq(PcMonthHoliday::getCompanyId, companyId)
                .eq(PcMonthHoliday::getDdate, date).count();
        if (count>0){
            throw new ServiceErrorException("日期存在");
        }
        /*组装数据*/
        PcMonthHoliday holiday = new PcMonthHoliday();
        holiday.setCompanyId(companyId);
        holiday.setDdate(date);
        holidayMapper.insert(holiday);
    }

    @Override
    public void delete(LocalDateTime date) {
        String companyId = UserIdThread.get().getCompanyId();
        /*删除指定数据*/
        LambdaQueryWrapper<PcMonthHoliday> wrapper = new LambdaQueryWrapper<PcMonthHoliday>()
                .eq(PcMonthHoliday::getCompanyId, companyId)
                .eq(PcMonthHoliday::getDdate, date);
        holidayMapper.delete(wrapper);
    }

    @Override
    public List<LocalDateTime> query(Integer year, Integer month) {
        String companyId = UserIdThread.get().getCompanyId();
        /*查询数据*/
        List<LocalDateTime> holidayList = holidayMapper.holidayQuery(year,month,companyId);
        return holidayList;

    }

    @Override
    public boolean holidayCheck(LocalDateTime date) {
        String companyId = UserIdThread.get().getCompanyId();
        /*判断是否为节假日*/
        LambdaQueryWrapper<PcMonthHoliday> wrapper = new LambdaQueryWrapper<PcMonthHoliday>()
                .eq(PcMonthHoliday::getCompanyId,companyId)
                .eq(PcMonthHoliday::getDdate,date);
        return holidayMapper.selectCount(wrapper) > 0;
    }
}
